package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.AttachmentInfo;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.FileType;
import com.jianyun.jyzs.utils.FileUtil;
import com.jianyun.jyzs.widget.ShowBigImageActivity;
import com.jianyun.jyzs.widget.voice.MediaManager;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/8/21.
 */

public class CrmAttachmentAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<AttachmentInfo> attachlist;

    public CrmAttachmentAdapter(Context context, List<AttachmentInfo> attachlist) {
        this.context = context;
        this.attachlist = attachlist;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_safety_enclosure_item, null);
        return new MyHolder(view);
    }

    public void setAttachlist(List<AttachmentInfo> attachlist) {
        this.attachlist = attachlist;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MyHolder myHolder = (MyHolder) holder;
        AttachmentInfo info = attachlist.get(position);
        RceLog.d("lml",info.toString());
        myHolder.tvEnclosureContentName.setText(info.getFilename());
        int type = info.getType();
        final String path = info.getPath();
        switch (type){
            case SysConstant.IMAGE:
                Picasso.with(context).load("file://"+path).error(R.drawable.error).into(myHolder.ivEnclosureContent);
                myHolder.linearLayoutArea.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, ShowBigImageActivity.class);
                        intent.putExtra("localpath", path);
                        context.startActivity(intent);
                    }
                });
                break;
            case SysConstant.VOICE:
                myHolder.ivEnclosureContent.setImageResource(R.drawable.enclosure_icon_mp3_pressed);
                myHolder.linearLayoutArea.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.voice_from_icon);
                        final AnimationDrawable anim = (AnimationDrawable) myHolder.ivEnclosureContent.getDrawable();
                        if (MediaManager.isPlay){
                            MediaManager.release();
                            anim.stop();
                            myHolder.ivEnclosureContent.setImageResource(R.drawable.enclosure_icon_mp3_pressed);
                            notifyDataSetChanged();
                        }else {
                            anim.start();
                            MediaManager.playSound(path, new MediaPlayer.OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    anim.stop();
                                    myHolder.ivEnclosureContent.setImageResource(R.drawable.enclosure_icon_mp3_pressed);
                                }
                            });
                        }

                    }
                });
                break;
            case SysConstant.VIDEO:
                Picasso.with(context).load(R.drawable.enclosure_icon_mp4_pressed).into(myHolder.ivEnclosureContent);
                myHolder.linearLayoutArea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse(path),"video/*");
                    context.startActivity(intent);
                }
            });
                break;
            case SysConstant.FILE:
//                Picasso.with(context).load(R.drawable.gongdanx_icon_file_pressed).into(myHolder.ivEnclosureContent);
                int fileType = FileUtil.getFileType(info.getFilename().toLowerCase());
                switch (fileType){
                    case FileType.IMAGE:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_insert_photo_black_48dp);
                        break;
                    case FileType.AUDIO:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_album_black_48dp);
                        break;
                    case FileType.VIDEO:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_theaters_black_48dp);
                        break;
                    case FileType.TEX:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_description_black_48dp);
                        break;
                    case FileType.OTHER:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_insert_drive_file_black_48dp);
                        break;
                    case FileType.WORD:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_word);
                        break;
                    case FileType.EXCEL:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_excel);
                        break;
                    case FileType.PDF:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_pdf);
                        break;
                    case FileType.RAR:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_ysb);
                        break;
                    case FileType.PPT:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_ppt);
                        break;
                    case FileType.DWG:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_dwg);
                        break;
                    default:
                        myHolder.ivEnclosureContent.setImageResource(R.drawable.ic_insert_drive_file_black_48dp);
                        break;
                }
                break;
        }

    }

    @Override
    public int getItemCount() {
        return attachlist==null?0:attachlist.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_enclosureContent)
        ImageView ivEnclosureContent;
        @BindView(R.id.tv_enclosureContent_name)
        TextView tvEnclosureContentName;
        @BindView(R.id.iv_deleteEnclosure)
        ImageView ivDeleteEnclosure;
        @BindView(R.id.linearLayout4)
        LinearLayout linearLayout4;
        @BindView(R.id.layout_praise_area)
        LinearLayout linearLayoutArea;


        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
